package com.lianzhihui.romeex.ui.trade;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.bean.FlowCellBean;
import com.lianzhihui.romeex.bean.PledgeRecordsBean;
import com.lianzhihui.romeex.config.AppConstants;
import com.lianzhihui.romeex.inter.DialogListener;
import com.lianzhihui.romeex.mvp.presenter.FlowCellInfoPresenter;
import com.lianzhihui.romeex.mvp.presenter.UpdatePresenter;
import com.lianzhihui.romeex.mvp.view.FlowCellView;
import com.lianzhihui.romeex.mvp.view.UpdateView;
import com.lianzhihui.romeex.ui.base.BaseActivity;
import com.lianzhihui.romeex.utils.MathUtil;
import com.lianzhihui.romeex.utils.NumberUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description:
 * Author:         RAMON
 * CreateDate:     2021/3/31 11:33
 * UpdateUser:
 * UpdateDate:
 * UpdateRemark:
 * Version:
 */
public class UpdateActivity extends BaseActivity implements FlowCellView, UpdateView {

    @BindView(R.id.ll_select)
    LinearLayout ll_select;
    @BindView(R.id.tv_xusdsum)
    TextView tv_xusdsum;
    @BindView(R.id.tv_rexsum)
    TextView tv_rexsum;
    @BindView(R.id.tv_pay1)
    TextView tv_pay1;
    @BindView(R.id.tv_pay2)
    TextView tv_pay2;

    FlowCellInfoPresenter infoPresenter;
    UpdatePresenter updatePresenter;
    PledgeRecordsBean.PledgeRecordBean bean;

    FlowCellBean config;
    int base;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_update_layout;
    }

    @Override
    protected void initView() {
        setTitle(getString(R.string.shengjizhiyajin));
        infoPresenter = new FlowCellInfoPresenter(this, this);
        updatePresenter = new UpdatePresenter(this, this);
        bean = (PledgeRecordsBean.PledgeRecordBean) getIntent().getSerializableExtra("bean");
        base = (int) (Double.parseDouble(bean.rusdBalance) / 100);
        showLoading("");
        infoPresenter.getFLInfo();
    }

    @Override
    protected void initData() {

    }

    public void showoption() {

        ArrayList<String> array = new ArrayList<>();

        for (int i = base + 1; i < 11; i++) {
            array.add(i + "00");
        }

        //条件选择器
        OptionsPickerView pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                tv_xusdsum.setText(array.get(options1));
                jisuan();
            }
        }).build();
        pvOptions.setPicker(array);
        pvOptions.show();
    }

    /**
     * 根据选择进行计算
     */
    private void jisuan() {

        if (config == null) {
            return;
        }

        double count = Double.parseDouble(tv_xusdsum.getText().toString());

        if (bean.status == 1 || bean.status == 2) {

            //需补充
            double percent = MathUtil.multiply(count, 0.3);
            double res = MathUtil.multiply(percent, config.getPrice());
            tv_rexsum.setText(NumberUtils.big8(res));

            double bu1 = count - Double.parseDouble(bean.rusdBalance);
            double percent2 = MathUtil.multiply(bu1, 0.3);
            double bu2 = MathUtil.multiply(percent2, config.getPrice());
            tv_pay1.setText(bu1 + "XUSD");
            tv_pay2.setText(bu2 + "REX");
        } else {
            tv_rexsum.setText("0");
            double bu1 = count - Double.parseDouble(bean.rusdBalance);
            tv_pay1.setText(bu1 + "XUSD");
            tv_pay2.setText("0 REX");
        }

    }

    @Override
    public void onFLInfo(FlowCellBean bean) {
        dismissLoading();
        //配置数据
        config = bean;
        tv_xusdsum.setText(base + 1 + "00");
        jisuan();

    }

    @Override
    public void onFLInfoFail(String message) {
        dismissLoading();
    }

    @Override
    public void setFailure() {

    }

    @Override
    public void onUpdate() {
        dismissLoading();
        toast("升级成功");
        sendMessage(AppConstants.Event.REFRESH_PLEDGE,new Object());
        finish();
    }

    @Override
    public void onUpdateFail(String message) {
        dismissLoading();
        toast("升级失败");
    }

    @OnClick({R.id.ll_select, R.id.bt_update})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_select:
                showoption();
                break;
            case R.id.bt_update:

                showSafty(new DialogListener(){

                    @Override
                    public void onPassed() {
                        showLoading();
                        updatePresenter.getUpdate(tv_xusdsum.getText().toString());
                    }

                    @Override
                    public void onFail() {

                    }
                });

                break;
        }
    }

}
